package com.scalar.chiptrack.operations.dataentry.inventoryReceive.actioncommands;

import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.InTransitLots;
import com.scalar.chiptrack.operations.dataentry.inventoryReceive.forms.InventoryReceiveForm;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.release.dao.RelaseDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: ChandraSekhar
 * Date: Fed 06, 2008
 * Time: 12:28:08 PM
 */
public class InventoryReceiveTransactionLoadLotNumbersCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/inventoryReceiveTrans.do" ;


    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {
        InventoryReceiveForm invReceiveForm = (InventoryReceiveForm) form;

        HashMap<String, String> errors = new HashMap<String, String>();
        HashMap<String, String> info = new HashMap<String, String>();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
       
        try
        {
            String previousPsType = NewProductDAO.getPreviousPsType( null, invReceiveForm.getProcessType() );
            String previousLocType = NewProductDAO.getBaseProcessName( null, previousPsType );
            String psTypeName = NewProductDAO.getBaseProcessName( null, invReceiveForm.getProcessType() );

            invReceiveForm.setBaseprocessType( psTypeName );
            
            if ( previousLocType.equalsIgnoreCase( LOCATION_TYPE_FAB ) || previousLocType.equalsIgnoreCase( LOCATION_TYPE_WAFER_STORE )
                    || previousLocType.equalsIgnoreCase( LOCATION_TYPE_SORT ) || "Back Grind".equalsIgnoreCase(invReceiveForm.getProcessType() ) )
            {
                 request.setAttribute("showWaferIds", "TRUE");
            }
            else
            {
                request.setAttribute("showWaferIds", "FALSE");
            }
            String psTyepStr = null;
            if ( psTypeName.equalsIgnoreCase( LOCATION_TYPE_DIE_BANK ) || psTypeName.equalsIgnoreCase( LOCATION_TYPE_WAFER_STORE)
                        || invReceiveForm.getProcessType().equalsIgnoreCase( LOCATION_TYPE_PRETEST ) || psTypeName.equalsIgnoreCase( LOCATION_TYPE_FINISHED_GOODS))
            {
                  psTyepStr = invReceiveForm.getProcessType() + ',' + previousPsType;
            }
            else
            {
                  psTyepStr =  previousPsType;
            }

            ArrayList<InTransitLots> transitLotList = DataEntryDAO.loadAllTransitLotsByPsType( psTyepStr, invReceiveForm.getProcessStepId() , invReceiveForm.getLocationId(), invReceiveForm.getProcessType(), user.getVendorId(), invReceiveForm.getLotId() );

            if ( transitLotList == null || transitLotList.size() == 0 )
            {
                info.put("No "+previousPsType+" Transit lots available" , null );
            }

            invReceiveForm.setTransitLotsList( transitLotList );
            
            request.setAttribute("lotsList",      transitLotList );
            request.setAttribute("nextLocations", RelaseDAO.loadProcessVendors( psTypeName ) );
            request.setAttribute("processTypeList", NewProductDAO.loadAllProcessesFromDB(null, false) );
            request.setAttribute("reciveProcessList", NewProductDAO.loadAllNextPsTypes( null, invReceiveForm.getProcessType(), true ) );
            request.setAttribute("partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( previousPsType ) );
            request.setAttribute("locationList",    VendorDAO.getInventoryLocationsByProcessByUserType( null, previousLocType, 0 )  );
            request.setAttribute("action", action);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return mapping.findForward("inventoryReceiveTrans");
    }
}